package hgx.spring.cloud.lession.productrepertory.hystrix.batch;/**
 * Created by liujun on 2018/4/21.
 */

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixRequestCache;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;

import java.util.ArrayList;
import java.util.List;

import hgx.spring.cloud.lession.productrepertory.api.ProductRepository;
import hgx.spring.cloud.lession.productrepertory.hystrix.ProductCommandEnum;
import hgx.spring.cloud.lession.productrepertory.vo.ProductVo;
import hgx.spring.cloud.lession.productrepertory.vo.ReponseProductVo;

/**
 * 查询所有产品
 * @author hugx
 * @create 2018-04-21 10:31
 */
public class ProductGetByIdBatchHystrixCommand extends HystrixCommand<List<ReponseProductVo>> {
    private ProductRepository productRepository;
    private List<Long> ids;
    private static final  HystrixCommandKey GetById_Key=HystrixCommandKey.Factory.asKey("product_getByIdBatch");

    public ProductGetByIdBatchHystrixCommand(ProductRepository productRepository, List<Long> ids) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(ProductCommandEnum.Product_Get_Set_GroupKey.getKey())).andCommandKey(GetById_Key));
        this.productRepository = productRepository;
        this.ids=ids;
    }


    @Override
    protected List<ReponseProductVo> run() throws Exception {
        long startTime = System.currentTimeMillis();
        System.out.println("执行 断路器命令的run() 方法 ,  keyName: " + getCommandKey().name());

        List<ReponseProductVo> reponse = new ArrayList<>();
        List<ProductVo> productVos = productRepository.findProductByIds(ids);
        if (productVos !=null) {
            for(ProductVo productVo : productVos) {
                ReponseProductVo reponseProductVo = new ReponseProductVo();
                reponseProductVo.setProductVo(productVo);
                reponseProductVo.success();
                reponse.add(reponseProductVo);
            }
        }

        System.out.println("3: ----------------run() ,耗时: " + (System.currentTimeMillis() - startTime));
        return reponse;
    }

    @Override
    protected List<ReponseProductVo> getFallback() {
        System.out.println("3.1: ----------------getFallback()  ");
        List<ReponseProductVo> reponse = new ArrayList<>(ids.size());
        for(int i=0;i<ids.size();i++){
            ReponseProductVo reponseProductVo=new ReponseProductVo();
            reponse.add(reponseProductVo);
        }

        return reponse;
    }

    @Override
    protected String getCacheKey() {
        /*根据查询条件置入缓存*/
        System.out.println("4.1-------------------------从缓存中获取数据");
        String result=String.valueOf(this.ids);
        System.out.println("4.2-------------------------从缓存中获取数据 ， cacheKey: "+result);
        return result;
    }

    public static void flushCache(Long ids){
        /*刷新缓坡，根据查询条件进行清理*/
        HystrixRequestCache.getInstance(GetById_Key, HystrixConcurrencyStrategyDefault.getInstance()).clear(String.valueOf(ids));
    }
}
